#include "grid.h"
#include "astar.h"
#include <iostream>
#include <vector>
#include <utility>

void printGrid(const Grid& grid, const std::vector<std::pair<int, int>>& path) {
	int rows = grid.getRows();
	int cols = grid.getCols();

	
	std::vector<std::vector<char>> displayGrid(rows, std::vector<char>(cols, '+'));

	//"#" for obstacles
	for (int row = 0; row < rows; ++row) {
		for (int col = 0; col < cols; ++col) {
			if (grid.isObstacle(row, col)) { 
				displayGrid[row][col] = '#';
			}
		}
	}

	// "*" for path cells
	for (const auto& p : path) {
		int row = p.first;
		int col = p.second;
		if (row >= 0 && row < rows && col >= 0 && col < cols) {
			displayGrid[row][col] = '*';
		}
	}

	
	
	for (const auto& row : displayGrid) {
		for (const auto& cell : row) {
			std::cout << cell << ' ';
		}
		std::cout << '\n';
	}
	
	std::cout << "(* for path cells)" << '\n';
	std::cout << "(# for obstacles)" << '\n';
}

int main() {
	
	Grid grid(4, 8);

	// this obstacle information can also be written into a file
	grid.addObstacle(0, 4);
	grid.addObstacle(0, 5);
	grid.addObstacle(0, 6);
	grid.addObstacle(0, 7);
	grid.addObstacle(1, 5);
	grid.addObstacle(1, 6);
	grid.addObstacle(1, 7);
	grid.addObstacle(2, 0);
	grid.addObstacle(2, 1);
	grid.addObstacle(2, 3);
	grid.addObstacle(2, 4);
	grid.addObstacle(3, 0);

	// find path
	AStar astar(grid);
	auto path = astar.findPath({0, 0}, {3, 7});

	// print path
	if (!path.empty()) {
		std::cout << "Path found:\n";
		printGrid(grid, path);
	} 
	else {
		std::cout << "No path found.\n";
	}
	
	return 0;
}
